package com;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

public class getservlet extends HttpServlet {
    /*
    一个项目只有一个ServletContext对象！

我们可以在N多个Servlet中来获取这个唯一的对象，使用它可以给多个Servlet传递数据！

与天地同寿！！！这个对象在Tomcat启动时就创建，在Tomcat关闭时才会死去
    * */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        System.out.println(context.getAttribute("name"));
        resp.getWriter().println(context.getAttribute("name"));
        //移除
        context.removeAttribute("name");
        HttpSession session = req.getSession();
        session.setAttribute("firsttime",new Date().toString().replaceAll(" ","-"));
        session.removeAttribute("firsttime");
        req.setAttribute("first","req");
        req.removeAttribute("firet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
